package ceura.ia.practica.android.listeners;

import android.view.MotionEvent;
import android.view.View;
import ceura.ia.practica.Point;
import ceura.ia.practica.android.brush.Brush;
import ceura.ia.practica.android.ui.BarraSuperior;
import ceura.ia.practica.android.ui.Celda;
import ceura.ia.practica.android.ui.LaberintoView;
import ceura.ia.practica.android.utilities.LaberintoUtils;
import ceura.ia.practica.properties.Propiedades;

/**
 * Listener de la vista del laberinto para escuchar eventos de pulsado y pintar
 * con el pincel que este seleccionado.
 *
 * @author 66785403 (Jorge Madrid Portillo)
 */
public class LaberintoTouchListener implements View.OnTouchListener, View.OnClickListener {

    private int posicionActual;
    private Celda celda;
    private Point tablePoint;
    private Propiedades properties;
    private LaberintoUtils utils;
    private LaberintoView laberintoView;
    private Brush brush;
    private BarraSuperior barraSuperior;
    private boolean canDragBrush = false;

    /**
     *
     * @param laberintoView La vista del laberinto.
     */
    public LaberintoTouchListener(LaberintoView laberintoView) {
        properties = Propiedades.getInstance();
        this.laberintoView = laberintoView;
        this.brush = laberintoView.getBrush();
        this.barraSuperior = laberintoView.getBarraSuperior();
        utils = new LaberintoUtils();
    }

    /**
     *
     * @param view La vista sobre la que se ha realizado el evento.
     * @param event El MotionEvent.
     * @return Si se debe continuar con la propagación del evento o no.
     */
    public boolean onTouch(View view, MotionEvent event) {
        this.brush = laberintoView.getBrush();

        switch (event.getAction()) {
            case MotionEvent.ACTION_OUTSIDE:
                break;
            case MotionEvent.ACTION_HOVER_ENTER:
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
            case MotionEvent.ACTION_HOVER_EXIT:
                break;
            case MotionEvent.ACTION_HOVER_MOVE:
                break;
            case MotionEvent.ACTION_MOVE:
                if (!laberintoView.isSelectionMode() && canDragBrush) {
                    tablePoint = new Point(utils.getXCelda(event.getX()), utils.getYCelda(event.getY()));
                    int position = utils.parsearTablaGrid(tablePoint);
                    if (position != posicionActual && utils.checkIsInLaberintoBounds(event,
                            properties.getLaberintoMargin())) {
                        paintCelda(tablePoint);
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                if (!laberintoView.isSelectionMode()) {
                    laberintoView.setSelectionMode(true);
                    barraSuperior.toggle();
                }
                break;
            case MotionEvent.ACTION_DOWN:
                if (!laberintoView.isSelectionMode()) {
                    tablePoint = new Point(utils.getXCelda(event.getX()), utils.getYCelda(event.getY()));
                    paintCelda(tablePoint);
                }
                break;
        }
        return false;
    }

    private void paintCelda(Point tablePoint) {
        posicionActual = utils.parsearTablaGrid(tablePoint);
        celda = (Celda) laberintoView.getCeldaAtPosition(posicionActual);
        canDragBrush = brush.paint(celda, laberintoView);
    }

    /**
     *
     * @param arg0
     */
    public void onClick(View arg0) {
    }
}
